<script setup>

import {Calendar, Search} from "@element-plus/icons-vue";
import {ref,reactive} from "vue";

import request from "@/utils/request.js";
import {ElMessage, ElMessageBox} from "element-plus";

const data = reactive({
  className: null,
  total:0,
  pageNum: 1,
  pageSize: 5,
  tableData: [],
  classId:null,
  FormVisible: false,
  form:{},
  rules:{
    className: [
      {required:  true, message: '请填写班级名称',trigger: 'blur'}
    ]

  },
  rows:[]

})
const formRef = ref()

const load = ()=>{
  request.get('schoolclass/selectPage',{
    params:{
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      className: data.className,
      classId:data.classId
    }

  }).then(res =>{
    console.log(res)
    data.tableData= res.data.list
    data.total= res.data.total

  })
}

load()

const reset = ()=>{
  data.classId=null
  data.className=null
  load()
}

reset()


const handleadd = () => {
  data.FormVisible = true
  data.form={}

}

const  add = () =>{
  formRef.value.validate((valid) => {
    if (valid){
      request.post("/schoolclass/add",data.form).then(res =>{

        if (res.code === '200') {
          data.FormVisible=false
          ElMessage.success("添加成功");
          load()

        } else {
          ElMessage.error(res.msg);

        }
      })
    }
  })

}

const handleEdit = (row) =>{
  data.form = JSON.parse(JSON.stringify(row))
  data.FormVisible = true

}

const update = () =>{
  request.put('schoolclass/update',data.form).then(res =>{
    if (res.code === '200') {
      data.FormVisible=false
      ElMessage.success("更新成功");
      load()

    } else {
      ElMessage.error(res.msg);

    }
  })
}

const save = () =>{
  data.form.classId ? update() : add()
}



const del = (id) =>{
  ElMessageBox.confirm(
      '你要确认删除该行数据吗？确认删除后，该数据无法恢复!!!!',
      '删除确认',
      {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
      }
  )
      .then(res => {
        request.delete('/schoolclass/delete/' + id).then(res =>{
          if (res.code === '200') {
            data.FormVisible=false
            ElMessage.success("删除成功");
            load()

          } else {
            ElMessage.error(res.msg);

          }

        })


      })
      .catch(error => {

      })

}

const handleSelectionChange = (rows) =>{
  data.rows = rows
}

const deleteBatch = ()=>{

  if (data.rows.length === 0){
    ElMessage.error("请选择需要删除的数据...")
  }else{
    ElMessageBox.confirm(
        '你要确认删除选中的数据吗？确认删除后，该数据无法恢复!!!!',
        '删除确认',
        {
          confirmButtonText: '删除',
          cancelButtonText: '取消',
          type: 'warning',
          center: true,
        }
    )
        .then(res => {
          request.delete('/schoolclass/deleteBatch', {data: data.rows}).then(res =>{
            if (res.code === '200') {
              data.FormVisible=false
              ElMessage.success("删除成功");
              load()
            } else {
              ElMessage.error(res.msg);

            }

          })


        })
        .catch(error => {

        })

  }


}


</script>

<template>

  <div>
    <div class="card" style="margin-bottom: 5px">
      <el-input clearable @clear='load' style="width: 260px; margin-right: 5px" v-model="data.className" placeholder="请输入班级名称查询" :prefix-icon="Search"></el-input>
      <el-input clearable @clear='load' style="width: 260px; margin-right: 5px" v-model="data.classId" placeholder="请输入班级编号查询" :prefix-icon="Search" ></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="primary" @click="reset">重置</el-button>

    </div>

    <div class="card" style="margin-bottom: 5px">
      <!--      <el-button type="danger" @click="handleadd">新增</el-button>-->
      <!-- Form -->
      <el-button  @click="handleadd" type="danger">新增</el-button>
      <el-button type="primary" @click="deleteBatch">批量删除</el-button>
      <el-button type="warning">批量导入</el-button>
      <el-button type="info">批量导出</el-button>
    </div>


    <div>
      <div class="card" style="margin-bottom: 5px">
        <el-table :data="data.tableData" stripe style="width: 100%" :header-cell-style="{color: '#333', backgroundColor: '#ddd'}"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection"  width="55" />
          <el-table-column prop="classId" label="班级编号"  />
          <el-table-column prop="className" label="班级名称" />
          <el-table-column prop="classAdvisor" label="班主任" />
          <el-table-column prop="monitor" label="班长"  />
          <el-table-column  label="修改/删除" width="100" >
            <template #default = "scope">
              <el-button type="primary" icon="Edit" circle @click="handleEdit(scope.row)"></el-button>
              <el-button type="danger" icon="Delete" circle @click="del(scope.row.classId)"></el-button>
            </template>

          </el-table-column>
        </el-table>
      </div>
    </div>

    <div class="card">

      <el-pagination
          v-model:current-page="data.pageNum"
          v-model:page-size="data.pageSize"
          :page-size="data.pageSize"
          :page-sizes="[5,10,20]"
          :pager-count="11"
          background layout="total, sizes, prev, pager, next, jumper"
          :total="data.total"
          @current-change="load"
          @size-change="load"

      />
    </div>

    <el-dialog v-model="data.FormVisible" title="新增管理员" center destroy-on-close width= 40%>
      <el-form ref="formRef" :model="data.form" :rules="data.rules" label-width= "80px" style="padding: 0px 20px 0 30px">
<!--        <el-form-item label="班级编号" prop="classId" >-->
<!--          <el-input v-model="data.form.username" autocomplete="off" />-->
<!--        </el-form-item>-->

        <el-form-item label="班级名称" prop="className" >
          <el-input v-model="data.form.className" autocomplete="off" />
        </el-form-item>

        <el-form-item label="班主任编号" prop="classAdvisor">
          <el-input v-model="data.form.classAdvisor" autocomplete="off" />
        </el-form-item>

        <el-form-item label="班长编号" prop="monitor">
          <el-input v-model="data.form.monitor" autocomplete="off" />
        </el-form-item>


      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.FormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </span>
      </template>
    </el-dialog>


  </div>


</template>


<style scoped>

</style>


